package com.hyy.lotterysystem.service.Impl;

import com.hyy.lotterysystem.controller.param.CreatePrizeParam;
import com.hyy.lotterysystem.controller.param.PageParam;
import com.hyy.lotterysystem.dao.mapper.PrizeMapper;
import com.hyy.lotterysystem.service.PictureService;
import com.hyy.lotterysystem.service.PriceService;
import com.hyy.lotterysystem.dao.dataobject.PrizeDO;
import com.hyy.lotterysystem.service.dto.PageListDTO;
import com.hyy.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PriceService {
    @Autowired
    private PictureService pictureService;

    @Autowired
    private PrizeMapper prizeMapper;

    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        //上传图片
       String fileName= pictureService.savePicture(picFile);

        //存库
        PrizeDO prize = new PrizeDO();
        prize.setName(param.getPrizeName());
        prize.setPrice(param.getPrice());
        prize.setDescription(param.getDescription());
        prize.setImageUrl(fileName);
        try{
            prizeMapper.insert(prize);

        }catch (Exception e){
            e.printStackTrace();
        }
        return prize.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {

        //总量
        int total=prizeMapper.count();

        //列表
        List<PrizeDTO> dtoList=new ArrayList<PrizeDTO>();
        List<PrizeDO> doList=prizeMapper.selectPrizeList(param.offset(),param.getPageSize());
        for(PrizeDO prize:doList){
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prize.getId());
            prizeDTO.setName(prize.getName());
            prizeDTO.setDescription(prize.getDescription());
            prizeDTO.setImageUrl(prize.getImageUrl());
            prizeDTO.setPrice(prize.getPrice());
            dtoList.add(prizeDTO);
        }
        return new PageListDTO<>(total,dtoList);
    }
}
